package org.example.memora.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.example.memora.common.result.Result;
import org.example.memora.common.utils.TagExtractor;
import org.example.memora.entity.Tag;
import org.example.memora.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/tags")
@io.swagger.v3.oas.annotations.tags.Tag(name = "标签管理", description = "提供标签管理的各种方法")
public class TagController {

    @Autowired
    private TagService tagService;

    @Operation(
            summary = "根据ID查询标签",
            description = "传入标签ID，从数据库获取该标签的详细信息"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功返回标签信息"),
            @ApiResponse(responseCode = "404", description = "未找到标签"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/{id}")
    public Result<Tag> getTagById(@PathVariable Long id) {
        Tag tag = tagService.getById(id);
        if (tag == null) {
            return Result.error("Tag not found");
        }
        return Result.success(tag);
    }

    @Operation(
            summary = "创建新标签",
            description = "传入标签名称，创建一个新的标签记录。若已存在同名标签则失败"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "标签已存在或创建失败"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/create")
    public Result<?> createTag(@RequestParam String name) {
        boolean success = tagService.createTag(name);
        if (!success) {
            return Result.error("标签已存在或创建失败");
        }
        return Result.success();
    }

    @Operation(
            summary = "禁用标签",
            description = "根据标签ID，将标签状态置为禁用"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "禁用成功"),
            @ApiResponse(responseCode = "404", description = "标签不存在"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/disable/{id}")
    public Result<?> disableTag(@PathVariable Long id) {
        boolean success = tagService.disableTag(id);
        if (!success) {
            return Result.error("标签不存在或禁用失败");
        }
        return Result.success();
    }

    @Operation(
            summary = "搜索标签",
            description = "通过关键字查询标签列表（模糊匹配），若未传则返回全部标签"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功返回标签列表"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/search")
    public Result<List<Tag>> searchTags(@RequestParam(required = false) String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            // 不传 keyword 就返回全部
            List<Tag> allTags = tagService.listAll();
            return Result.error(allTags);
        }
        List<Tag> list = tagService.getTagsByName(keyword);
        return Result.success(list);
    }

    @Operation(
            summary = "文本中自动提取 #标签#",
            description = "从传入的文本（正文）中识别出 #标签# 并返回标签列表"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功提取标签列表"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/extract")
    public Result<List<String>> extract(@RequestBody String content) {
        List<String> tags = TagExtractor.extractTagsFromContent(content);
        tagService.createTags(tags);
        return Result.success(tags);
    }
}
